'use strict';
var getMonth = function(month, isChinese) {
    if (month > 0 && month <= 12) {
        switch (month) {

            case 1:
                {
                    return isChinese ? "1月" : "Jan";
                }
            case 2:
                {
                    return isChinese ? "2月" : "Feb";
                }
            case 3:
                {
                    return isChinese ? "3月" : "Mar";
                }
            case 4:
                {
                    return isChinese ? "4月" : "Apr";
                }
            case 5:
                {
                    return isChinese ? "5月" : "May";
                }
            case 6:
                {
                    return isChinese ? "6月" : "Jun";
                }
            case 7:
                {
                    return isChinese ? "7月" : "Jul";
                }
            case 8:
                {
                    return isChinese ? "8月" : "Aug";
                }
            case 9:
                {
                    return isChinese ? "9月" : "Sep";
                }
            case 10:
                {
                    return isChinese ? "10月" : "Oct";
                }
            case 11:
                {
                    return isChinese ? "11月" : "Nov";
                }
            case 12:
                {
                    return isChinese ? "12月" : "Dec";
                }
        }
    }
}
let choose_year = null,
    choose_month = null;
const conf = {
    data: {
        hasEmptyGrid: false,
        showPicker: false
    },
    onShow() {
        const date = new Date();
        const cur_year = date.getFullYear();
        const cur_month = date.getMonth() + 1;
        const weeks_ch = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
        this.setData({
            isChinese: wx.getStorageSync("isChinese")
        })
        wx.setNavigationBarTitle({
            title: this.data.isChinese ? "日期选择" : "Date selection"
        })
        var years = wx.getStorageSync("years");
        for (let i = 0; i < years.length; i++) {
            years[i].select = false
        }

        wx.setStorageSync("years", years)


        // getMonth(cur_month, this.data.isChinese)
        this.setData({
            cur_year,
            month: getMonth(cur_month, this.data.isChinese),
            cur_month: cur_month,
            weeks_ch,
            years: years
        });
        this.calculateEmptyGrids(cur_year, cur_month);
        this.calculateDays(cur_year, cur_month);
    },
    getThisMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
    },
    getFirstDayOfWeek(year, month) {
        return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    calculateEmptyGrids(year, month) {
        const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
        let empytGrids = [];
        if (firstDayOfWeek > 0) {
            for (let i = 0; i < firstDayOfWeek; i++) {
                empytGrids.push(i);
            }
            this.setData({
                hasEmptyGrid: true,
                empytGrids
            });
        } else {
            this.setData({
                hasEmptyGrid: false,
                empytGrids: []
            });
        }
    },
    calculateDays(year, month) {
        let days = [],
            years = this.data.years;
        const thisMonthDays = this.getThisMonthDays(year, month);
        for (let i = 1; i <= thisMonthDays; i++) {
            var newObj = {}
            newObj.day = i
            newObj.choosed = false
            newObj.disable = false
            for (let j = 0; j < years.length; j++) {
                if (years[j].dates == String(year) + String(month > 10 ? month : '0' + month) + String(i > 10 ? i : '0' + i)) {
                    newObj.disable = true
                    if (years[j].select) { //已选择日期。重新赋值渲染页面
                        newObj.choosed = true
                    }
                }
            }

            days.push(newObj);
        }
        this.setData({
            days
        });

    },
    handleCalendar(e) {
        const handle = e.currentTarget.dataset.handle;
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        // getMonth(cur_month, this.data.isChinese)
        if (handle === 'prev') {
            let newMonth = cur_month - 1;
            let newYear = cur_year;
            if (newMonth < 1) {
                newYear = cur_year - 1;
                newMonth = 12;
            }
            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);
            this.setData({
                cur_year: newYear,
                month: getMonth(newMonth, this.data.isChinese),
                cur_month: newMonth
            });

        } else {
            let newMonth = cur_month + 1;
            let newYear = cur_year;
            if (newMonth > 12) {
                newYear = cur_year + 1;
                newMonth = 1;
            }
            this.calculateDays(newYear, newMonth);
            this.calculateEmptyGrids(newYear, newMonth);

            this.setData({
                cur_year: newYear,
                month: getMonth(newMonth, this.data.isChinese),
                cur_month: newMonth
            });
        }
    },
    tapDayItem(e) {
        const idx = e.currentTarget.dataset.idx;
        const days = this.data.days;
        if (days[idx].disable) {
            const day = days[idx].day
            days[idx].choosed = !days[idx].choosed;
            this.isSelect(day, days[idx].choosed)
            this.setData({
                days,
            });
        }

    },
    isSelect(day, select) { //点击可选择日期的操作
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        const years = this.data.years
        for (let i = 0; i < years.length; i++) {
            if (years[i].dates == String(cur_year) + String(cur_month > 10 ? cur_month : '0' + cur_month) + String(day > 10 ? day : '0' + day)) {
                years[i].select = select
            }
        }
        wx.setStorageSync('years', years)


    },
    chooseYearAndMonth() {
        const cur_year = this.data.cur_year;
        const cur_month = this.data.cur_month;
        let picker_year = [],
            picker_month = [];
        for (let i = 1900; i <= 2100; i++) {
            picker_year.push(i);
        }
        var x;
        for (let i = 1; i <= 12; i++) {
            picker_month.push(i);
        }
        const idx_year = picker_year.indexOf(cur_year);
        const idx_month = picker_month.indexOf(cur_month);
        this.setData({
            picker_value: [idx_year, idx_month],
            picker_year,
            picker_month,
            showPicker: true,
        });
    },
    pickerChange(e) {
        const val = e.detail.value;
        choose_year = this.data.picker_year[val[0]];
        choose_month = this.data.picker_month[val[1]];
    },
    tapPickerBtn(e) {
        const type = e.currentTarget.dataset.type;
        const o = {
            showPicker: false,
        };
        if (type === 'confirm') {
            o.cur_year = choose_year;
            o.cur_month = choose_month;
            this.calculateEmptyGrids(choose_year, choose_month);
            this.calculateDays(choose_year, choose_month);
        }
        this.setData(o);
    },
    onHide(e) {

    },
    search() {
        wx.redirectTo({
            url: '../searchActivity/searchActivity',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    }
};

Page(conf);